package io.mft.commons.multithreading.worker;

import java.util.concurrent.Future;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 工作对象
 * @描述: 
 * @版权: Copyright (c) 2015 
 * @公司: 思迪科技 
 * @作者:胡常建
 * @版本: 1.0 
 * @创建日期: 2015年5月17日 
 * @创建时间: 上午9:38:04
 */
public class Worker  implements Runnable {
	private Future<?> runner;
	private String serialNo;
	private String poolId;
	public String getSerialNo() {
		return serialNo;
	}
	static Logger logger=LoggerFactory.getLogger(Worker.class);
    private  final WorkQueue workQueue;
    
    private CompleteWorkListener listener;
    public Worker(String poolId,String serialNo,WorkQueue workQueue,CompleteWorkListener listener){
    	this.serialNo=serialNo;
    	this.workQueue=workQueue;
    	this.listener=listener;
    	this.poolId=poolId;
    }
	@Override
	public void run() {
		Thread.currentThread().setName("工作线程["+poolId+"-"+serialNo+"]");
		while (true) {
			try {
				Work work= workQueue.take();
				if (work!=null) {
					Object object=work.handle();
					WorkResult workResult=new WorkResult(Worker.this,work,object);
					listener.onCompleteWork(Worker.this, work, workResult);
				}
			} catch (Exception e) {
				logger.error("", e);
			}
		}
	}
	public Future<?> getRunner() {
		return runner;
	}
	public void setRunner(Future<?> runner) {
		this.runner = runner;
	}
}
